<template>
    <view class="content">
		<image src="../../static/image/logo.png" class="logo"></image>
        <view class="btn" @click="login">授权登录</view>
    </view>
</template>

<script>
    import {
        ajaxPost,
        isNull,
        getSync,
        getParam,
        setSync
    } from '../../static/js/common.js'
    export default {
        data() {
            return {
                type: 1,
				from: '',
				url: '',
            }
        },
        onLoad(option) {
			let _this = this
			if (option.from) {
				// 从表单页进入、分享
				this.from = option.from
			}
            
			this.url = window.location.href;
			if (!getParam('code')) {
			    window.location.href =
			        'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx552129804846d230&redirect_uri=' +
			        encodeURIComponent(this.url) + '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
			}
			console.log('url', this.url)
			console.log('code', getParam('code'))
        },
        methods: {
            login() {
				const _this = this;
				uni.showLoading({
					title: '登录中'
				});
                
                if (getParam('code')) {
                    let code = getParam('code');
                    _this.code(code)
                }
				uni.hideLoading()
            },

            code(code) {
                ajaxPost('weixin', {
                    code: code, //微信授权码
                }).then(res => {
                    console.log('weixin', res)
                    if (res.status == 0) {
                        uni.setStorageSync('userToken', res.data.userToken); //存token
                        
                        uni.showModal({
                            title: '提示',
                            content: '登录成功',
                            showCancel: false,
                            confirmColor: '#00b6bc',
                            success: (val) => {
                                if (val.confirm) {
                        			if (this.from == 1) {
                        				this.$cTo(`/pages/settle/question`)
                        			} else if (this.from == 2) {
										this.$cTo(`/pages/card/card`)
									} else {
                        				if (window.location.href.indexOf("?#") < 0) {
                        				    window.location.href = 'http://official.yinuo1228.com/h5/?#/';
                        				}
                        			}
                                }
                            }
                        });
                    }
                })
            },
        },
    }
</script>

<style>
    .wximg {
        width: 90rpx;
        height: 90rpx;
    }

    page {
        width: 100vw;
        background-color: #FFFFFF;
        padding: 10vw;
        box-sizing: border-box;

    }

    .content {
        display: flex;
        justify-content: center;
        flex-direction: column;
    }

    .logo {
        width: 99upx;
        height: 107upx;
        margin: 0 auto;
    }

    .btn {
        width: 40vw;
        height: 90upx;
        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
        background-color: #00b6bc;
        font-size: 32upx;
        color: #fff;
        border-radius: 30px;
        margin: 60upx auto;
    }
</style>